package com.lorgeoux.android.parking.maps;

import java.util.Locale;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.android.maps.OverlayItem;
import com.lorgeoux.android.BaseMapActivity;
import com.lorgeoux.android.R;
import com.readystatesoftware.mapviewballoons.BalloonOverlayView;

public class ParkingBalloonOverlayView<Item extends OverlayItem> extends BalloonOverlayView<ParkingOverlayItem> {

	private TextView title;
	private TextView snippet;
	private BaseMapActivity activity;
	private String telephone;
	private String adresseParking;
	private Context context;

	public ParkingBalloonOverlayView(Context context, int balloonBottomOffset) {
		super(context, balloonBottomOffset);
	}

	@Override
	protected void setupView(Context context, final ViewGroup parent) {

		this.context = context;
		
		// inflate our custom layout into parent
		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View v = inflater.inflate(R.layout.parking_balloon_overlay, parent);

		// setup our fields
		title = (TextView) v.findViewById(R.id.balloon_item_title);
		snippet = (TextView) v.findViewById(R.id.balloon_item_snippet);

		// implement balloon close
		ImageView close = (ImageView) v.findViewById(R.id.balloon_close);
		close.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				parent.setVisibility(GONE);
			}
		});
		
		// implement telephoner
		ImageView telephone = (ImageView) v.findViewById(R.id.kf_call_start);
		telephone.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if (ParkingBalloonOverlayView.this.telephone != null && !ParkingBalloonOverlayView.this.telephone.equals("")) {
					Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + ParkingBalloonOverlayView.this.telephone));
					activity.startActivity(intent);
				} else {
					BaseMapActivity.afficherPopupAppelImpossible(ParkingBalloonOverlayView.this.context);
				}
			}
		});

		// implement appel navigator
		ImageView navigator = (ImageView) v.findViewById(R.id.icon_navigator);
		navigator.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				// Convertion nom du parking pour recherche google.navigation
				String[] adresseParking = ParkingBalloonOverlayView.this.adresseParking.split(" ");
				
				StringBuilder uri = new StringBuilder("google.navigation:q=NANTES");
				for (String mot : adresseParking) {
					uri.append("+").append(mot.toUpperCase(Locale.getDefault()));
				}
			    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri.toString()));
			    activity.startActivity(intent); 
			}
		});

	}

	@Override
	protected void setBalloonData(ParkingOverlayItem item, ViewGroup parent) {

		// map our custom item data to fields
		title.setText(item.getTitle());
		snippet.setText(item.getSnippet());
		ParkingBalloonOverlayView.this.adresseParking = item.getEquipementsPublicMobilite().getAdresse();
		ParkingBalloonOverlayView.this.telephone = item.getEquipementsPublicMobilite().getTelephone();
		ParkingBalloonOverlayView.this.activity = (BaseMapActivity)item.getActivity();

	}

}
